#ifndef SQLSERVER_H
#define SQLSERVER_H

#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QStack>
#include <QDateTime>
#include <QSqlRecord>
#include <QMutex>
#include <QTimer>
#include <QFile>

QT_BEGIN_NAMESPACE
namespace Ui { class SqlServer; }
QT_END_NAMESPACE

class SqlServer : public QMainWindow
{
    Q_OBJECT

public:
    SqlServer(QWidget *parent = nullptr);
    ~SqlServer();

private slots:
    void newConnection();
    void readData();
    void clientDisconnected();
    QString handleAuthRequest(const QString &sql);

private:
    Ui::SqlServer *ui;
    QTcpServer *tcpServer;
    QStack<QSqlDatabase> connectionPool;
    QMutex poolMutex;
    QTimer *connectionCheckTimer;
    int maxPoolSize = 20;

    bool initializeDatabase();
    bool createConnection(int id);
    QSqlDatabase getConnection();
    void releaseConnection(QSqlDatabase &db);
    QString executeQuery(const QString &sql);
    void logMessage(const QString &message);
    void checkConnections();
};

#endif // SQLSERVER_H
